var AlignmentTool = Class.create({
	initialize: function()
	{
		window.tool = this;
		
		this.lh_surface = $('lhscontainer');
		this.rh_surface = $('rhs');
		this.info_surface = $('rhs');
		
		this.data = new DataStructure(window.data);
		
		this.ungrouped_list = new UngroupedList(this.data);
		this.lh_surface.insert(this.ungrouped_list.getSurface());

		this.grouped_list = new GroupedList(this.data);		
		this.lh_surface.insert(this.grouped_list.getSurface());
		
		this.infoview = new InfoView();
		this.info_surface.insert(this.infoview.getSurface());
		
		// this.grouped_list.addItems(this.data.slice(10, 14));
		// this.grouped_list.addItems(this.data.slice(100, 102));
		
		// Create keyboard shortcuts for functions
		
		Event.observe(window, 'keydown', function(event){
			if(event.keyCode == 77) // m
				document.fire('unmatchedlist:match');
			else if(event.keyCode == 83) // s
				document.fire('data:save');
			
			if(event.keyCode == 38 /* UP */ || event.keyCode == 40 /* DOWN */ || event.keyCode == 39 /* RIGHT */)
			{
				this.ungrouped_list.keyPress(event);
				event.stop();
			}
				
			//console.debug(event);
		}.bind(this));
		
		$('loading').hide();
		
		document.observe('loading:show', function(event) { this.showLoading(event.memo) }.bindAsEventListener(this));
		document.observe('loading:hide', this.hideLoading.bindAsEventListener(this));
		
		if(window.location.hash)
		{
			var h = window.location.hash.substr(1, window.location.hash.length-1);
			this.hash = h.toQueryParams();
		}
		else
			this.hash = {};
		
		if(this.hash.skin)
		{
			var fileref=document.createElement("link");
			fileref.setAttribute("rel", "stylesheet");
			fileref.setAttribute("type", "text/css");
			fileref.setAttribute("href", "css/"+this.hash.skin+".css");
			document.getElementsByTagName("head")[0].appendChild(fileref);
		}
	},
	
	showLoading: function(string)
	{
		if(string && Object.isString(string))
			$('loading').update(string);
			
		$('loading').show();
	},
	
	hideLoading: function()
	{
		$('loading').hide();
	}
});

document.observe('dom:loaded', function(){
	new AlignmentTool();
})